package ar.com.photo_admin.activity;

import com.nostra13.universalimageloader.core.ImageLoader;

import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.widget.Toast;
import ar.com.photo_admin.R;
import ar.com.photo_admin.services.ServiceLayer;
import ar.com.photo_admin.services.Services;
import ar.com.photo_admin.services.ServiceLayerImpl.TokenOperationResponse;

public class SettingsActivity extends PreferenceActivity {

	private ServiceLayer service;
	
	private ImageLoader imageLoader = ImageLoader.getInstance();
	
	@SuppressWarnings("deprecation")
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);
        
        service = Services.createServiceLayer(this);
		
        Preference clearEvents = (Preference) findPreference("clearEvents");
        clearEvents.setOnPreferenceClickListener(new OnPreferenceClickListener() {
	         public boolean onPreferenceClick(Preference preference) {
	             
	        	TokenOperationResponse deleteAllTokens = service.deleteAllTokens();
				Toast.makeText(SettingsActivity.this, deleteAllTokens.message, Toast.LENGTH_SHORT).show();
	        	 
	        	return true;
	         }
	    });
        
        Preference clearCache = (Preference) findPreference("clearCache");
        clearCache.setOnPreferenceClickListener(new OnPreferenceClickListener() {
	         public boolean onPreferenceClick(Preference preference) {
	             
	        	String result = getString(R.string.clear_image_cache_success);
					
				try {
					imageLoader.clearMemoryCache();
					imageLoader.clearDiscCache();	
				} catch (Exception e) {
					result = getString(R.string.clear_image_cache_fail);
				}
				
				Toast.makeText(SettingsActivity.this, result, Toast.LENGTH_SHORT).show();
        	 
				return true;
	         }
	    });
    }
}